<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class News_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();		
		$this->_table = $this->config->item('database_tables');
	}
	
	public function get_all_category()
	{
		$this->db->select('*');
		$this->db->from('site_category');
		$this->db->where('status', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_id_category_sitemap()
	{
		$this->db->select('id');
		$this->db->from('site_category');
		$this->db->where('status', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_content_of_category($alias, $limit, $start)
	{
		$this->db->select('site_content.id, site_content.title, site_content.alias, site_content.description, site_content.category_id, site_content.content, 
		site_content.content_image, site_content.creator_id, site_content.expired_date, site_content.created_date, site_content.view, site_content.featured, site_content.status, 
		site_category.id as site_categoryid, site_category.name, site_category.alias as site_categoryalias, site_category.description as catdescription ');
		$this->db->from('site_content','site_category');
		$this->db->join('site_category', 'site_category.id = site_content.category_id');
		$this->db->where('site_content.status', 1);
		$this->db->where('site_category.alias', $alias);
		$this->db->order_by('site_content.id','DESC');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
		
	}
	
	public function get_page($alias)
	{
		$this->db->select('*');
		$this->db->where('alias',$alias);
		$query = $this->db->get('site_page');
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result[0];
		}
		return null;
	}
	
	
	public function count_all_content($id)
	{
		$this->db->select('id');
		$this->db->from('site_content');
		$this->db->where('category_id', $id);
		return $this->db->count_all_results();
	}
	
	public function get_article_content($alias)
	{
		$this->db->select('*');
		$this->db->where('status', 1);
		$this->db->where('alias', $alias);
		$this->db->limit(1);
		$query = $this->db->get('site_content');
		$this->update_view($alias);		
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result[0];
		}
		return null;
	}
	
	public function get_related_items($id)
	{
		$obj = $this->get_product($id);
		$catid = $obj['category_id'];
		return $this->get_related_in_category($catid, 5);
		
	}
	
	public function get_related_content($catid, $alias)
	{
		$this->db->select('site_content.id, site_content.title, site_content.alias, site_content.description, site_content.category_id, site_content.content, 
		site_content.content_image, site_content.creator_id, site_content.expired_date, site_content.created_date, site_content.view, site_content.featured, site_content.status, 
		site_category.id as site_categoryid, site_category.name, site_category.alias as site_categoryalias, site_category.description as catdescription ');
		$this->db->from('site_content','site_category');
		$this->db->join('site_category', 'site_category.id = site_content.category_id');
		$this->db->where('site_content.status', 1);
		$this->db->where('site_content.category_id',$catid);
		$this->db->where_not_in('site_category.alias', $alias);
		$this->db->order_by('site_content.id','random');
		$this->db->limit(5);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	public function get_related_in_category($id, $limit=0)
	{
		$this->db->select('id, product_name, description, created_by, app_id, category_id, hit, icon, featured, status, price');
		$this->db->where('status', 1);
		$this->db->where('category_id', $id);
		if($limit!=0)
		{
			$this->db->limit($limit);
		}
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get('product');
		return $query->result_array();
	}
	
	function update_view($id)
	{
		$this->db->where('alias', $id);
    	$this->db->set('view', 'view+1', FALSE);
    	$this->db->update('site_content');
	}
	
	public function dangkyhoc($id)
	{
		$data = array(
			'id_nganh'  =>  $id,
			'tennganh'	=>	$this->input->post('tennganh'),
			'hovaten'	=>	$this->input->post('tenhv'),
			'diachi'	=>	$this->input->post('diachi'),
			'didong'	=>	$this->input->post('didong'),
		);
		
		$this->db->insert('dangkyhoc', $data);
	}
	// =============================
	// = ========== GET ========== =
	// =============================
	
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
}
// END homepage_model.php
/* Location: ./application/modules/home/models/homepage_model.php */